<?php

/**
 * @file classes/journal/SDTKDAO.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class SDTKDAO
 * @ingroup journal
 * @see SDTK
 *
 * @brief Operations for retrieving and modifying SDTK objects.
 */

// $Id$

define("SDTK_FIELD_MATK", "matk");
define("SDTK_FIELD_MADV", "madv");
define("SDTK_FIELD_NONE", null);



import ('classes.acct.SDTK');

class SDTKDAO extends DAO {
	var $cache;

	function _cacheMiss(&$cache, $id) {
		$SDTK =& $this->getSDTK($id, null, false);
		$cache->setCache($id, $SDTK);
		return $SDTK;
	}

	function &_getCache() {
		if (!isset($this->cache)) {
			$cacheManager =& CacheManager::getManager();
			$this->cache =& $cacheManager->getObjectCache('SDTK', 0, array(&$this, '_cacheMiss'));
		}
		return $this->cache;
	}

 	function &zgetSDTK($nam, $MATK, $DaoFac = false, $combo = false) {
		if (isset($MATK))
			$result =& $this->retrieve(
				'SELECT * FROM SDTK'.$nam." WHERE MATK = ? ORDER BY matk', $MATK"
			);
		else
			$result =& $this->retrieve(
				"SELECT * FROM SDTK".$nam." WHERE matk !='' ORDER BY matk");



		$sectionsIterator = new DAOResultFactory($result, $this, '_returnSDTKFromRow');

		if ($DaoFac)
			return $sectionsIterator;
		else 
		{
			$returner = null;
			if ($result->RecordCount() != 0) {
				$returner =& $this->_returnSDTKFromRow($result->GetRowAssoc(false));
			}

			$result->Close();
			unset($result);
			return $returner;
		}
	}

	


	function &_returnSDTKFromRow(&$row) {
		$SDTK = new SDTK();
		$SDTK->setid($row['id']);
		$SDTK->setmatk($row['matk']);
		$SDTK->settentk($row['tentk']);
		$SDTK->setdknovn($row['dknovn']);
		$SDTK->setdkcovn($row['dkcovn']);
		return $SDTK;
	}




	function &getSDTKSearch($nam, $searchType = null, $search = null, $searchMatch = null) {

		$paramArray = array();
		$searchSql = '';

		$searchTypeMap = array(
			SDTK_FIELD_MATK => 'matk',
			SDTK_FIELD_MADV => 'tentk'
		);

		if (!empty($search) && isset($searchTypeMap[constant($searchType)])) {
			$fieldName = $searchTypeMap[constant($searchType)];
			switch ($searchMatch) {
				case 'is':
					$searchSql = "AND LOWER($fieldName) = LOWER(?)";
					$paramArray[] = $search;
					break;
				case 'contains':
					$searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
					$paramArray[] = '%' . $search . '%';
					break;
				case 'startsWith':
					$searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
					$paramArray[] = $search . '%';
					break;
			}
		}


//		elseif (!empty($search)) switch ($searchType) {
//			case USER_FIELD_USERID:
//				$searchSql = 'AND u.user_id=?';
//				$paramArray[] = $search;
//				break;
//			case USER_FIELD_INITIAL:
//				$searchSql = 'AND LOWER(u.last_name) LIKE LOWER(?)';
//				$paramArray[] = $search . '%';
//				break;
//		}

		//$searchSql .= ($sortBy?(' ORDER BY ' . $this->getSortMapping($sortBy) . ' ' . $this->getDirectionMapping($sortDirection)) : '');



		$result =& $this->retrieve(
				"SELECT a.* FROM SDTK".$nam.
			" a WHERE matk !='' ". $searchSql. " ORDER BY matk",
			$paramArray
		);


		$returner = new DAOResultFactory($result, $this, '_returnSDTKFromRow');
		
		return $returner;
	}



	/**
	 * Insert a new SDTK.
	 * @param $SDTK SDTK
	 */
	function insertSDTK(&$SDTK, $nam) {
		$this->update(
			'INSERT INTO SDTK'.$nam.' 
				(matk, mant)
				VALUES
				(?, ?)',
			array(
				$SDTK->getmatk(),
				$SDTK->getmant()
			)
		);

//		$SDTK->setId($this->getInsertSDTKId());
//		$this->updateLocaleFields($SDTK);
//		return $SDTK->getId();
	}

	function ExistTable($Year) {
		$sql = "select 1 from SDTK".$Year;
		$result =& $this->retrieve($sql);
		return $result;
	}
	
	function CreateTable($Year, $journalid ) {
		$sql = "CREATE TABLE SDTK".$journalid.$Year." LIKE SDTKstr" ;
		$result =& $this->retrieve($sql );

		return;

	}


	function deleteSDTK($nam, $id) {
		return $this->update(
			"DELETE FROM SDTK$nam WHERE id = ? ",
			array($id)
			);
	}


	/**
	 * Update an existing SDTK.
	 * @param $SDTK SDTK
	 */
	function updateSDTK(&$SDTK) {
		$returner = $this->update(
			'UPDATE SDTK
				SET
					matk = ?,
					tentk = ?
				WHERE matk = ?',
			array(
				$SDTK->getmatk(),
				$SDTK->gettentk(),
				$SDTK->getmatk()
			)
		);
		$this->updateLocaleFields($SDTK);
		return $returner;
	}

}

?>
